package service.impl;

import java.util.Date;
import java.util.List;

import persistence.dao.TdfkfaDocDAO;
import persistence.dao.TdfkfaFileDAO;
import persistence.model.TdfkfaDoc;
import persistence.model.TdfkfaDocExample;
import persistence.model.TdfkfaFile;
import presentation.extendmodel.DocDetailModel;
import presentation.queryvo.DocQueryVO;
import service.DocService;

public class DocServiceImpl implements DocService {

    private TdfkfaDocDAO tdfkfaDocDAO;

    private TdfkfaFileDAO tdfkfaFileDAO;

    public List<TdfkfaDoc> getDocList(DocQueryVO queryVO, int pageIndex, int pageSize) {

        TdfkfaDocExample example = new TdfkfaDocExample();
        example.setOrderByClause("create_date desc");
        List<TdfkfaDoc> list = tdfkfaDocDAO.selectDocListByExample(example, pageIndex, pageSize);
        return list;
    }
    
    public int countDoc(DocQueryVO queryVO) {
        
        TdfkfaDocExample example = new TdfkfaDocExample();
        return tdfkfaDocDAO.countByExample(example);
    }

    public DocDetailModel getDocDetail(Integer docId) {

        DocDetailModel docDetail = new DocDetailModel();
        TdfkfaDoc doc = tdfkfaDocDAO.selectByPrimaryKey(docId);
        docDetail.setDoc(doc);

        List<TdfkfaFile> fileList = tdfkfaFileDAO.selectByDocId(docId);
        docDetail.setAttachmentFileList(fileList);

        return docDetail;
    }

    public void saveDoc(String title, String content) {

        TdfkfaDoc record = new TdfkfaDoc();

        record.setTitle(title);
        record.setContent(content);
        Date now = new Date();
        record.setCreateDate(now);
        record.setUpdateDate(now);

        tdfkfaDocDAO.insertSelective(record);
    }

    public void deleteDoc(Integer[] docId) {
        for (Integer bid : docId) {
            tdfkfaDocDAO.deleteByPrimaryKey(bid);
        }
    }
    
    public void updateDoc(String docId, String docTitle, String docContent) {
        TdfkfaDoc tdfkfaDoc = new TdfkfaDoc();
        tdfkfaDoc.setDid(new Integer(docId));
        tdfkfaDoc.setTitle(docTitle);
        tdfkfaDoc.setContent(docContent);
        tdfkfaDoc.setUpdateDate(new Date());
        tdfkfaDocDAO.updateByPrimaryKeySelective(tdfkfaDoc);
    }

    public TdfkfaDoc getDoc(Integer docId) {
        return tdfkfaDocDAO.selectByPrimaryKey(docId);
    }

    public void setTdfkfaDocDAO(TdfkfaDocDAO tdfkfaDocDAO) {
        this.tdfkfaDocDAO = tdfkfaDocDAO;
    }

    public void setTdfkfaFileDAO(TdfkfaFileDAO tdfkfaFileDAO) {
        this.tdfkfaFileDAO = tdfkfaFileDAO;
    }

}
